package cn.itsource.smartcampus.service.impl;

import cn.itsource.smartcampus.domain.User;
import cn.itsource.smartcampus.exception.GlobalException;
import cn.itsource.smartcampus.mapper.UserMapper;
import cn.itsource.smartcampus.service.UserService;
import cn.itsource.smartcampus.util.TokenMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper umapper;
    @Override
    public String login(User user) throws GlobalException {
        String name = user.getName();
        String pwd = user.getPwd();
        //判断用户名是否为null或者空字符串
        if("".equals(name) || name == null){
            throw new GlobalException("用户名不能为空");
        }
        //判断密码是否为null或者空字符串
        if("".equals(pwd) || pwd == null){
            throw new GlobalException("密码不能为空");
        }

        User login = umapper.login(user);
        //判断是否登录成功
        if(login==null){
            throw new GlobalException("用户名或者密码错误");
        }
        //登录成功 生成一个令牌 保存到map集合中
        String token = UUID.randomUUID().toString();
        TokenMap.setTokenMap(token,login);
        return token;

    }
}
